
#include "Engine.h"
#include "Input.h"
#include "Camera.h"
#include "TEString.h"
#include "Socket.h"

// Reads Settings
#ifdef USE_SETTINGS
void ReadSettings();
uint ReadGeneral(vector<string> &toks, uint i);
uint ReadDisplay(vector<string> &toks, uint i);
uint ReadControls(vector<string> &toks, uint i);
uint ReadEnvironment(vector<string> &toks, uint i);
#endif

//
// Process Message
//

LRESULT WINAPI MsgProc( HWND window, UINT msg, WPARAM wParam, LPARAM lParam )
{
	if(msg & SERVER_DATA)
	{
		ServerData(wParam, lParam, msg & !SERVER_DATA);
	}
	else
	{
		switch( msg )
		{
		case WM_DESTROY:
			PostQuitMessage( 0 );
			return 0;

		case WM_ACTIVATE:
			if(WA_INACTIVE != wParam)
				input.AcquireDevices();
			break;

		// Sockets
		case SERVER_ACCEPT:
			ServerAccept(wParam, lParam);
			break;

		case CLIENT_DATA:
			ClientData(wParam, lParam);
			break;
		}
	}

	return DefWindowProc(window, msg, wParam, lParam);
}

//
// Main method
//

int WINAPI wWinMain( HINSTANCE hInst, HINSTANCE, LPWSTR, INT )
{
	// Set default settings
	//engine.camera->SetFullScreen(true);
	//engine.camera->SetResolution(1920, 1200);
	engine.camera->SetFullScreen(false);
	engine.camera->SetResolution(1280, 800);
	//engine.camera->SetFullScreen(false);
	//engine.camera->SetResolution(1024, 600);

	// Try to read in settings file
	#ifdef USE_SETTINGS
	ReadSettings();
	#endif

	// Register the window class
	WNDCLASSEX wc = { sizeof(WNDCLASSEX), CS_CLASSDC, MsgProc, 0L, 0L, 
										GetModuleHandle(NULL), LoadIcon(NULL, IDI_WINLOGO), 
										LoadCursor(NULL, IDC_ARROW), NULL, NULL,
										L"Target Engine", NULL };
	RegisterClassEx(&wc);

	// Create the application's window
	HWND window;
	if(engine.camera->FullScreen())
	{
		window = CreateWindow(L"Target Engine", L"Target Engine", 
													WS_POPUP, 00, 00, GetSystemMetrics(SM_CXSCREEN), 
													GetSystemMetrics(SM_CYSCREEN),
													NULL, NULL, wc.hInstance, NULL);
	}
	else
	{
		window = CreateWindow(L"Target Engine", L"Target Engine", 
													WS_OVERLAPPEDWINDOW, 00, 00, engine.camera->Width(), 
													engine.camera->Height(),
													NULL, NULL, wc.hInstance, NULL);
	}

	// Initialize Direct3D & enter the main loop if successful
	if(engine.Initialize(window))
		engine.MainLoop();

	// Unregister resources
	UnregisterClass( L"Target Engine", wc.hInstance );
	return 0;
}

//
// Reads in settings file
//

#ifdef USE_SETTINGS
void ReadSettings()
{
	vector<string> toks;
	ifstream file("..\\bin\\settings.ini", ifstream::in);

	if(file.is_open())
	{
		// Scan the data
		string line;
		while(file.good())
		{
			getline(file, line);
			Tokenize(line, toks, " =");
		}

		// Close the file
		file.close();

		// Process data
		uint i = 0;
		while(i < toks.size())
		{
			if(toks[i] == "[General]")       i = ReadGeneral(toks, i);
			else if(toks[i] == "[Display]")  i = ReadDisplay(toks, i);
			else if(toks[i] == "[Controls]") i = ReadControls(toks, i);
			//else if(toks[i] == "[Audio]") ;
			else if(toks[i] == "[Environment]") i = ReadEnvironment(toks, i);
			//else if(toks[i] == "[Other]") ;

			++i;
		}
	}
}

uint ReadGeneral(vector<string> &toks, uint i)
{
	++i;

	while(i < toks.size() && toks[i][0] != '[')
	{
		if(toks[i] == "Name")
		{
			engine.player.SetName(toks[++i]);
		}

		++i;
	}

	return i-1;
}

uint ReadDisplay(vector<string> &toks, uint i)
{
	++i;

	while(i < toks.size() && toks[i][0] != '[')
	{
		if(toks[i] == "Fullscreen")
		{
			bool fullscreen = GetInt(toks[++i]) != 0;
			engine.camera->SetFullScreen(fullscreen);
			//MessageBox(NULL, (fullscreen ? L"TRUE" : L"FALSE"), L"", MB_OK);
		}
		else if(toks[i] == "Width")
		{
			ushort width = GetInt(toks[++i]);
			engine.camera->SetWidth(width);
		}
		else if(toks[i] == "Height")
		{
			ushort height = GetInt(toks[++i]);
			engine.camera->SetHeight(height);
		}

		++i;
	}

	return i-1;
}

uint ReadControls(vector<string> &toks, uint i)
{
	++i;

	while(i < toks.size() && toks[i][0] != '[')
	{
		if(toks[i] == "Speed")
		{
			float speed = GetFloat(toks[++i]);
			engine.camera->linearSpeed = speed;
		}
		else if(toks[i] == "MouseXSensitivity")
		{
			short x = GetInt(toks[++i]);
			engine.camera->xSensitivity = x;
		}
		else if(toks[i] == "MouseYSensitivity")
		{
			short y = GetInt(toks[++i]);
			engine.camera->ySensitivity = y;
		}

		++i;
	}

	return i-1;
}


uint ReadEnvironment(vector<string> &toks, uint i)
{
	++i;

	while(i < toks.size() && toks[i][0] != '[')
	{
		if(toks[i] == "Gravity")
		{
			float gravity = GetFloat(toks[++i]);
			engine.gravity = gravity;
		}

		++i;
	}

	return i-1;
}
#endif

